 ;              Asm Functions
 ;            by Andrew F. Davis


 [BITS 32]

 SECTION .text

 GLOBAL _getkey
 GLOBAL _setup_gdt
 GLOBAL _setup_idt
 GLOBAL _isr0
 GLOBAL _isr1
 GLOBAL _isr2
 GLOBAL _isr3
 GLOBAL _isr4
 GLOBAL _isr5
 GLOBAL _isr6
 GLOBAL _isr7
 GLOBAL _isr8
 GLOBAL _isr9
 GLOBAL _isr10
 GLOBAL _isr11
 GLOBAL _isr12
 GLOBAL _isr13
 GLOBAL _isr14
 GLOBAL _isr15
 GLOBAL _isr16
 GLOBAL _isr17
 GLOBAL _isr18
 GLOBAL _isr19
 GLOBAL _isr20
 GLOBAL _isr21
 GLOBAL _isr22
 GLOBAL _isr23
 GLOBAL _isr24
 GLOBAL _isr25
 GLOBAL _isr26
 GLOBAL _isr27
 GLOBAL _isr28
 GLOBAL _isr29
 GLOBAL _isr30
 GLOBAL _isr31
 GLOBAL _isr32
 GLOBAL _isr33
 GLOBAL _isr34
 GLOBAL _isr35
 GLOBAL _isr36
 GLOBAL _isr37
 GLOBAL _isr38
 GLOBAL _isr39
 GLOBAL _isr40
 GLOBAL _isr41
 GLOBAL _isr42
 GLOBAL _isr43
 GLOBAL _isr44
 GLOBAL _isr45
 GLOBAL _isr46
 GLOBAL _isr47
 GLOBAL _isr48
 GLOBAL _isr64
 GLOBAL _int_handler_set
 GLOBAL _breakpoint
 GLOBAL _int30
 GLOBAL _task_timer
 GLOBAL _setup_tss

 EXTERN _gp
 EXTERN _idtp
 EXTERN _isr_handler
 EXTERN _timer_handler

 KERNEL_DATA_SEL  equ  0x10

 GLOBAL _curr_task
 EXTERN _RUNNING_PROCESS
 EXTERN _TSS

 _setup_gdt:
     lgdt [_gp]
     mov ax, 0x10
     mov ds, ax
     mov es, ax
     mov fs, ax
     mov gs, ax
     jmp 0x08:flush
 flush:
     ret

 _setup_idt:
     lidt [_idtp]
     ret

 _setup_tss:
     push ax
     mov ax, 5 * 8 ;GDT #5
     ltr ax
     pop ax

 _isr0:
     cli
     push byte 0
     push byte 0
     jmp isr
 _isr1:
     cli
     push byte 0
     push byte 1
     jmp isr
 _isr2:
     cli
     push byte 0
     push byte 2
     jmp isr
 _isr3:
     cli
     push byte 0
     push byte 3
     jmp isr
 _isr4:
     cli
     push byte 0
     push byte 4
     jmp isr
 _isr5:
     cli
     push byte 0
     push byte 5
     jmp isr
 _isr6:
     cli
     push byte 0
     push byte 6
     jmp isr
 _isr7:
     cli
     push byte 0
     push byte 7
     jmp isr
 _isr8:
     cli
     push byte 8
     jmp isr
 _isr9:
     cli
     push byte 0
     push byte 9
     jmp isr
 _isr10:
     cli
     push byte 10
     jmp isr
 _isr11:
     cli
     push byte 11
     jmp isr
 _isr12:
     cli
     push byte 12
     jmp isr
 _isr13:
     cli
     push byte 13
     jmp isr
 _isr14:
     cli
     push byte 14
     jmp isr
 _isr15:
     cli
     push byte 0
     push byte 15
     jmp isr
 _isr16:
     cli
     push byte 0
     push byte 16
     jmp isr
 _isr17:
     cli
     push byte 0
     push byte 17
     jmp isr
 _isr18:
     cli
     push byte 0
     push byte 18
     jmp isr
 _isr19:
     cli
     push byte 0
     push byte 19
     jmp isr
 _isr20:
     cli
     push byte 0
     push byte 20
     jmp isr
 _isr21:
     cli
     push byte 0
     push byte 21
     jmp isr
 _isr22:
     cli
     push byte 0
     push byte 22
     jmp isr
 _isr23:
     cli
     push byte 0
     push byte 23
     jmp isr
 _isr24:
     cli
     push byte 0
     push byte 24
     jmp isr
 _isr25:
     cli
     push byte 0
     push byte 25
     jmp isr
 _isr26:
     cli
     push byte 0
     push byte 26
     jmp isr
 _isr27:
     cli
     push byte 0
     push byte 27
     jmp isr
 _isr28:
     cli
     push byte 0
     push byte 28
     jmp isr
 _isr29:
     cli
     push byte 0
     push byte 29
     jmp isr
 _isr30:
     cli
     push byte 0
     push byte 30
     jmp isr
 _isr31:
     cli
     push byte 0
     push byte 31
     jmp isr
  _isr32:
     cli
     push byte 0
     push byte 32
     jmp isr
 _isr33:
     cli
     push byte 0
     push byte 33
     jmp isr
 _isr34:
     cli
     push byte 0
     push byte 34
     jmp isr
 _isr35:
     cli
     push byte 0
     push byte 35
     jmp isr
 _isr36:
     cli
     push byte 0
     push byte 36
     jmp isr
 _isr37:
     cli
     push byte 0
     push byte 37
     jmp isr
 _isr38:
     cli
     push byte 0
     push byte 38
     jmp isr
 _isr39:
     cli
     push byte 0
     push byte 39
     jmp isr
 _isr40:
     cli
     push byte 0
     push byte 40
     jmp isr
 _isr41:
     cli
     push byte 0
     push byte 41
     jmp isr
 _isr42:
     cli
     push byte 0
     push byte 42
     jmp isr
 _isr43:
     cli
     push byte 0
     push byte 43
     jmp isr
 _isr44:
     cli
     push byte 0
     push byte 44
     jmp isr
 _isr45:
     cli
     push byte 0
     push byte 45
     jmp isr
 _isr46:
     cli
     push byte 0
     push byte 46
     jmp isr
 _isr47:
     cli
     push byte 0
     push byte 47
     jmp isr
 _isr48:
     cli
     push byte 0
     push byte 48
     jmp isr
 _isr64:
     cli
     push byte 0
     push byte 64
     jmp isr

 isr:
;     cli
     pusha
     push ds
     push es
     push fs
     push gs
     mov ax, 0x10
     mov ds, ax
     mov es, ax
     mov fs, ax
     mov gs, ax
     mov ebx, esp
     push ebx
     call _isr_handler
     add esp, 4
;    test ebx, eax
;    je noswitch
;    mov edx, _RUNNING_PROCESS
;    cmp dword[edx], 0x00
;    je notss
;    mov ecx, [_TSS]
;    mov word [ecx+8], KERNEL_DATA_SEL
;    mov edx, [edx+8]
;    add edx, 4096
;    mov dword[ecx+4], edx
 notss:
     mov esp, eax
 noswitch:
     pop gs
     pop fs
     pop es
     pop ds
     popad
     add esp, 8
     sti
     iretd

 _breakpoint:
     iret

